package com.axin.blog.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.axin.blog.api.domain.Blog;
import com.axin.blog.api.domain.BlogCategory;
import com.axin.blog.api.domain.BlogTag;
import com.axin.blog.mapper.BlogCategoryMapper;
import com.axin.blog.mapper.BlogTagMapper;
import com.axin.blog.service.IBlogCategoryService;
import com.axin.blog.service.IBlogService;
import com.axin.blog.service.IBlogTagService;
import com.axin.common.core.domain.web.TableDataInfo;
import com.axin.common.mybatis.utils.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 博客分类 服务层实现
 *
 * @Author axin
 * @Date 2021/10/29 17:08
 **/
@Service
public class BlogTagServiceImpl extends ServiceImpl<BlogTagMapper, BlogTag> implements IBlogTagService {

    @Autowired
    private IBlogService blogService;


    /**
     * 根据参数分页查询博客标签列表
     *
     * @param tag
     * @return
     */
    @Override
    public TableDataInfo selectPageBlogTagList(BlogTag tag) {
        return PageUtils.buildDataInfo(baseMapper.selectPageBlogTagList(PageUtils.buildPage(), tag));
    }

    /**
     * 查询博客标签列表
     *
     * @param tag
     * @return
     */
    @Override
    public List<BlogTag> selectBlogTagList(BlogTag tag){
        return baseMapper.selectBlogTagList(tag);
    }

    /**
     * 新增博客标签
     *
     * @param tag
     * @return 结果
     */
    @Override
    public int insertBlogTag(BlogTag tag) {
        return baseMapper.insert(tag);
    }


    /**
     * 查询标签信息
     *
     * @param tagId
     * @return
     */
    @Override
    public BlogTag selectBlogTagById(Long tagId){
        return baseMapper.selectById(tagId);
    }

    /**
     * 修改标签信息
     *
     * @param tag 分类信息
     * @return 结果
     */
    @Override
    public int updateBlogTag(BlogTag tag){
        return baseMapper.updateById(tag);
    }

    /**
     * 批量删除标签
     *
     * @param tagIds 需要删除的分类ID
     * @return 结果
     */
    @Override
    public void deleteTagByIds(Long[] tagIds){
        for (Long tagId : tagIds) {
            BlogTag tag = selectBlogTagById(tagId);
            // todo  判断标签下是否有博客
        }
        baseMapper.deleteBatchIds(Arrays.asList(tagIds));
    }


    /**
     * 校验标签名是否唯一
     *
     * @param tag
     * @return 结果
     */
    @Override
    public boolean checkTagNameUnique(BlogTag tag){
        Long tagId = ObjectUtil.isNull(tag.getTagId()) ? -1L : (tag.getTagId());
        BlogTag info = baseMapper.selectOne(new LambdaQueryWrapper<BlogTag>().eq(BlogTag::getTagName, tag.getTagName()));
        if (ObjectUtil.isNotNull(info) && info.getTagId().longValue() != tagId.longValue()) {
            return false;
        }
        return true;
    }

    /***********************************************************前台接口**********************************************/

    /**
     * 获取标签归档
     * @return
     */
    @Override
    public List<BlogTag> getTagArchiveList() {
        // 查询开启的标签
        LambdaQueryWrapper<BlogTag> lqw = new LambdaQueryWrapper<BlogTag>()
                .eq(BlogTag::getStatus, 1)
                .orderByAsc(BlogTag::getSortNum);
        List<BlogTag> tagList = baseMapper.selectList(lqw);

        // 统计标签下的博客数量
        tagList.forEach(item ->
                item.setCountNum(blogService.count(new LambdaQueryWrapper<Blog>().like(Blog::getTagIds, item.getTagId())))
        );

        // 按照博客数量降序
        tagList.sort(Comparator.comparing(BlogTag::getCountNum).reversed());

        return tagList;
    }


}
